package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strings"
	"sync"
)

func httpGet() {
	resp, err := http.Get("http://10.84.135.139:8089/inter/xpvbvan/inter?method=config&type=set&value=[]&key=wap_recommend")
	if err != nil {
		// handle error
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		// handle error
	}

	//fmt.Println(string(body))
	fmt.Println("request done", body)
}

// 编码类型为：application/json
func httpPost(requestBody, url, USER_UID, USER_IS_LOGIN string) {
	var jsonStr = []byte(requestBody)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("USER_UID", USER_UID)
	req.Header.Add("USER_IS_LOGIN", USER_IS_LOGIN)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	fmt.Println("response url:", url)
	fmt.Println("response Status:", resp.Status)
	//fmt.Println("response Headers:", resp.Header)
	//body, _ := ioutil.ReadAll(resp.Body)
	//fmt.Println("response Body:", string(body))
}

// 编码类型为：application/json
func HttpGet(url string) {
	req, err := http.NewRequest("GET", url, nil)
	req.Header.Add("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	fmt.Println("response url:", url)
	fmt.Println("response Status:", resp.Status)
	loger.Println("response url:", url)// 使用的时候，需要采用loger作为输出的前缀
	loger.Println("response Status:", resp.Status)// 使用的时候，需要采用loger作为输出的前缀

}




// 编码为 application/x-www-form-urlencoded
func httpDo() {
	client := &http.Client{}
	const uriBase = "http://10.84.135.139:8089"
	const uriPath = "/inter/xpsfsdfsdgan/inter?method=batchcashlist"
	const url = uriBase + uriPath
	req, err := http.NewRequest("POST", url, strings.NewReader("pids=1829587349042601,985162418600072,985162418617402"))
	if err != nil {
		// handle error
	}

	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("USER_UID", "4093715774")
	req.Header.Add("USER_IS_LOGIN", "1")

	resp, err := client.Do(req)

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		// handle error
	}
	fmt.Print(resp.StatusCode)
	fmt.Println(string(body))
}

// 并发测试方法
func testBingfa(n int) {
	requestBody := `{
        "pid": "985162418722700",
        "proname": "这是",
        "bussid": "1",
        "anchorname":"这是",
        "imgurl": "https://pcsdata.hello.com/thumbnail/e7235204f92202431714eb0d47ef3ad8?fid=2073169351-250528-355546491061776&rt=pr&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-KFRL8PgNZVaZg0lE%2Fe%2BlSuJuTDY%3D&expires=2h&chkv=0&chkbd=0&chkpc=&dp-logid=1656043183&dp-callid=0&time=1554706800&size=c200_u200&quality=100&vuk=-&ft=video",
        "prourl":"https://pasdfgsdgn.hello.com/mall/wap/home#/albumList?pid=1266637396195769&skuid=",
        "theme_id":"6"
      }`
	url := "https://www.baidu.com/"
	USER_UID := "2222342870"
	USER_IS_LOGIN := "1"
	var wg sync.WaitGroup
	wg.Add(n)
	for i := 0; i < n; i++ {
		go func() {
			url := url
			httpPost(requestBody, url, USER_UID, USER_IS_LOGIN) //被测试方法
			wg.Done()
		}()

		//httpPost(requestBody, url, USER_UID, USER_IS_LOGIN) // 不并发请求
	}
	wg.Wait()
	fmt.Println("ending----")
}

// 并发测试方法
func testBingfa_get(n int) {
	url := "https://www.baidu.com/"
	var wg sync.WaitGroup
	wg.Add(n)
	for i := 0; i < n; i++ {
		loger.Println("第几个goroutine:", i)// 使用的时候，需要采用loger作为输出的前缀

		go func() {
			url := url
			HttpGet(url) //被测试方法

			wg.Done()
		}()
		//HttpGet(url) // 不并发请求
	}
	wg.Wait()
	fmt.Println("ending----")
}





var loger *log.Logger

func init() {
	file := "./" + "Mylog" + ".txt"
	//file := "./" + time.Now().Format("20180102") + ".txt"
	logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	if err != nil {
		panic(err)
	}
	loger = log.New(logFile, "[qSkiptool]",log.LstdFlags | log.Lshortfile | log.LUTC) // 将文件设置为loger作为输出
	return
}



func main() {
	//testBingfa(50)
	testBingfa_get(100)
}